package me.pinion.model;

import me.pinion.Log;
import me.pinion.model.annotation.TableBind;

import java.util.List;

@TableBind(table = "role")
public class Role extends XModel<Role>{
    public static Role dao = new Role();

    public Role byAuthority(String authority){
        Role role = this.findFirst("SELECT * FROM role WHERE authority=?", authority);
        if (role == null){
            role = new Role();
            role.set("authority", authority);
            role.save();
        }
        return role;
    }

    public List<Role> inAuthorities(String[] authorities){
        StringBuilder authoritiesBuilder = new StringBuilder();
        for (String authority: authorities)
            authoritiesBuilder.append(authority +  ",");
        authoritiesBuilder.deleteCharAt(authoritiesBuilder.length() - 1);
        List<Role> roles = this.find("SELECT * FROM role WHERE authority in (?)", authoritiesBuilder.toString());
        Log.i(roles.size());
        return roles;
    }
}
